package com.lcu.bos.web.action;

import com.lcu.bos.dao.ISubareaDao;
import com.lcu.bos.modle.PageBean;
import com.lcu.bos.modle.Region;
import com.lcu.bos.modle.Subarea;
import com.lcu.bos.service.ISubareaService;
import com.lcu.bos.web.action.base.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class SubareaAction extends BaseAction<Subarea> {
    @Autowired
    private ISubareaService subareaService;
    @Override
    public String save() {
        System.out.println("数据1"+getModel());
        subareaService.save(getModel());
        return SUCCESS;
    }

    @Override
    public String delete() throws IOException {
        return null;
    }

    @Override
    public String update() throws UnsupportedEncodingException {
        return null;
    }

    @Override
    public String get() {
        return null;
    }

    @Override
    public String findAll() {
        return null;
    }
    /*分页查询，前台参数使用get方法获取，set方法设置*/
    private int page;
    private int rows;
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getRows() {
        return rows;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }
    public void pageQuery() throws IOException {
        //1.接收参数 page当前页 rows【每页显示多少条数据】
        PageBean<Subarea> pb=new PageBean<>();
        //2.调用service
        //封装条件
        pb.setCurrentPage(page);
        pb.setPageSize(rows);
        DetachedCriteria cd=DetachedCriteria.forClass(Subarea.class);//离线查询条件

        //通过离线查询对象 添加查询的条件
        String addresskey=getModel().getAddresskey();
        if(!StringUtils.isEmpty(addresskey)){
            cd.add(Restrictions.like("addresskey","%"+addresskey+"%"));
        }
        Region region=getModel().getRegion();
        if (region!=null){
            String province=region.getProvince();
            String city=region.getCity();
            String district=region.getDistrict();
            System.out.println(province+city+district+"=========");
            cd.createAlias("region","r");
            if(!StringUtils.isEmpty(province)){
                cd.add(Restrictions.like("r.province","%"+addresskey+"%"));
            }
            if(!StringUtils.isEmpty(city)){
                cd.add(Restrictions.like("r.city","%"+addresskey+"%"));
            }
            if(!StringUtils.isEmpty(district)){
                cd.add(Restrictions.like("r.district","%"+addresskey+"%"));
            }
        }

        pb.setDetachedCriteria(cd);
        subareaService.pageQuery(pb);
        //3.返回json格式数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        /*获取数据的时候，把分区Subarea的Region加载方式懒加载设置为即时加载*/
        config.setExcludes(new String[]{"currentPage","pageSize","detachedCriteria","subareas","decidedzone"});//排出Region中的subareas因为会显示Region
        JSONObject jb=JSONObject.fromObject(pb,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
    //导出 xls表格文件
    public void exportExcel() throws IOException {
       //准备xls文件[输入流]
        //1.创建wookbook
        HSSFWorkbook work=new HSSFWorkbook();
        //2.创建sheet
        HSSFSheet sheet = work.createSheet("sheet1");
        //3.创建标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("分区编码");
        row.createCell(1).setCellValue("区域编码");
        row.createCell(2).setCellValue("关键字");
        row.createCell(3).setCellValue("省市区");

        //查询数据
        List<Subarea> subareas = subareaService.findAll();
        for (Subarea s:subareas){
            row=sheet.createRow(sheet.getLastRowNum()+1);
            row.createCell(0).setCellValue(s.getId());
            row.createCell(1).setCellValue(s.getRegion().getId());
            row.createCell(2).setCellValue(s.getAddresskey());
            row.createCell(3).setCellValue(s.getRegion().getName());
        }
        //响应输出[输出流]
        HttpServletResponse reponse=ServletActionContext.getResponse();
        //设置响应头
        String filename=URLEncoder.encode("分区表格.xls","utf-8");
        reponse.setHeader("Content-Disposition","attachment;filename="+filename);
        String contentType=ServletActionContext.getServletContext().getMimeType(filename);
        reponse.setContentType(contentType);  //根据名称转类型
        //reponse.setContentType("application/octet-stream");
        OutputStream os=reponse.getOutputStream();
        work.write(os);
    }
    public void listJson() throws IOException {
        //获取未绑定的分区数据【也就是这个分区没有指派一个员工负责】
        List<Subarea> list=subareaService.findAllWithUbind();
        //3.返回json格式数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        config.setExcludes(new String[]{"decidedzone","region"});
        JSONArray jb=JSONArray.fromObject(list,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
}
